package com.ng.desktop.hr.view.customer;

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.Arrays;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import org.apache.commons.lang.StringUtils;

import com.ng.desktop.hr.model.Customer;
import com.ng.desktop.hr.model.Email;
import com.ng.desktop.hr.model.Phone;
import com.ng.desktop.hr.persistence.CustomerPersistence;
import com.ng.framework.view.desktop.NGCombo;
import com.ng.framework.view.desktop.NGDatePicker;
import com.ng.framework.view.desktop.NGEditDialog;
import com.ng.framework.view.desktop.NGEntitySelectorField;

public class CustomerEditDialog extends NGEditDialog<Customer, CustomerPersistence> {

	private static final long serialVersionUID = -8491323457814105705L;

	private JTextField textFieldId;
	private JTextField textFieldNombre;
	private JTextField textFieldApellido;
	private JTextField textFieldEmail;
	private JTextField textFieldTelefono;
	private NGCombo cmbPendiente;
	private JTextArea textAreaObservaciones;

	public CustomerEditDialog(Frame owner, CustomerPersistence persistence,
			NGEntitySelectorField<Customer, CustomerPersistence> entitySelection) {
		this(owner, persistence, null, entitySelection);
	}

	public CustomerEditDialog(Frame owner, CustomerPersistence persistence, Customer instance,
			NGEntitySelectorField<Customer, CustomerPersistence> entitySelection) {
		super(owner, persistence, instance, entitySelection);

		this.setTitle("Gestión de Clientes");
		this.setSize(new Dimension(400, 350));
	}

	@Override
	protected JPanel createDetailPanel() {

		this.textFieldId = new JTextField(5);
		textFieldId.setEditable(false);
		textFieldId.setFocusable(false);
		textFieldId.setHorizontalAlignment(JTextField.RIGHT);
		this.textFieldNombre = new JTextField();
		this.textFieldApellido = new JTextField();
		textFieldApellido.setHorizontalAlignment(JTextField.LEFT);
		this.textFieldEmail = new JTextField();
		textFieldEmail.setHorizontalAlignment(JTextField.LEFT);
		this.textFieldTelefono = new JTextField();
		textFieldTelefono.setHorizontalAlignment(JTextField.LEFT);
		this.cmbPendiente = new NGCombo();
		this.textAreaObservaciones = new JTextArea();

		JPanel detailPanel = new JPanel(new GridBagLayout());

		detailPanel.add(new JLabel("Id:"), new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
		detailPanel.add(textFieldId, new GridBagConstraints(1, 0, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.NONE,
				new Insets(0, 0, 5, 0), 0, 0));

		detailPanel.add(new JLabel("Nombre:"), new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
		detailPanel.add(textFieldNombre, new GridBagConstraints(1, 1, 1, 1, 1, 0, GridBagConstraints.WEST,
				GridBagConstraints.HORIZONTAL, new Insets(0, 0, 5, 0), 0, 0));

		detailPanel.add(new JLabel("Apellido:"), new GridBagConstraints(0, 2, 1, 1, 0, 0, GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
		detailPanel.add(textFieldApellido, new GridBagConstraints(1, 2, 1, 1, 1, 0, GridBagConstraints.WEST,
				GridBagConstraints.HORIZONTAL, new Insets(0, 0, 5, 0), 0, 0));

		detailPanel.add(new JLabel("Email:"), new GridBagConstraints(0, 3, 1, 1, 0, 0, GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
		detailPanel.add(textFieldEmail, new GridBagConstraints(1, 3, 1, 1, 1, 0, GridBagConstraints.WEST,
				GridBagConstraints.HORIZONTAL, new Insets(0, 0, 5, 0), 0, 0));

		detailPanel.add(new JLabel("Teléfono:"), new GridBagConstraints(0, 4, 1, 1, 0, 0, GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
		detailPanel.add(textFieldTelefono, new GridBagConstraints(1, 4, 1, 1, 1, 0, GridBagConstraints.WEST,
				GridBagConstraints.HORIZONTAL, new Insets(0, 0, 5, 0), 0, 0));

		detailPanel.add(new JLabel("Pendiente:"), new GridBagConstraints(0, 5, 1, 1, 0, 0, GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(0, 0, 0, 5), 0, 0));
		detailPanel.add(cmbPendiente, new GridBagConstraints(1, 5, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.NONE,
				new Insets(0, 0, 0, 0), 0, 0));

		detailPanel.add(new JLabel("Observaciones:"), new GridBagConstraints(0, 6, 1, 1, 0, 0, GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(0, 0, 0, 5), 0, 0));
		detailPanel.add(textAreaObservaciones, new GridBagConstraints(0, 7, 2, 5, 1, 1, GridBagConstraints.WEST,
				GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));

		cmbPendiente.addNGItems(Arrays.asList("Si", "No"));

		// List<Tag> rightTags = new ArrayList();
		// List<Tag> leftTags = tagPersistence.getAll();
		// cmbPendiente.addNGItems(Arrays.asList("Si", "No"));
		//
		// if (modoEdicion) {
		// tfId.setText(String.valueOf(contacto.getId()));
		// tfNombre.setText(contacto.getName());
		// tfApellido.setText((contacto.getLastName()));
		// cmbPendiente.setSelectedNGItem(contacto.getPending() ? "Si" : "No");
		// tfEmail.setText(contacto.getPrimaryEmail() == null ? "" : contacto
		// .getPrimaryEmail().getLiteral());
		// tfTelefono.setText(contacto.getPrimaryPhone() == null
		// ? ""
		// : contacto.getPrimaryPhone().getNumber());
		// rightTags.addAll(contacto.getTags());
		// tfaObservaciones.setText(contacto.getObservations());
		// }
		//
		// pickTags.setElements(leftTags, rightTags);

		return detailPanel;
	}

	@Override
	protected boolean validateInput() {

		boolean valid = true;

		if (StringUtils.isEmpty(textFieldNombre.getText())) {
			JOptionPane.showMessageDialog(this, "El campo 'Nombre' es obligatorio.", "Error", JOptionPane.ERROR_MESSAGE);
			textFieldNombre.requestFocus();

			valid = false;
		} else if (StringUtils.isEmpty(textFieldApellido.getText())) {
			JOptionPane.showMessageDialog(this, "El campo 'Apellido' es obligatorio.", "Error", JOptionPane.ERROR_MESSAGE);
			textFieldApellido.requestFocus();

			valid = false;

		} else if (StringUtils.isEmpty(textFieldEmail.getText())) {
			JOptionPane.showMessageDialog(this, "El campo 'Email' es obligatorio.", "Error", JOptionPane.ERROR_MESSAGE);
			textFieldEmail.requestFocus();

			valid = false;
		}
		return valid;
	}

	@Override
	protected Customer createNewInstance() {
		return new Customer();
	}

	@Override
	protected void populateInstance(Customer instance) {

		if (!isCreationMode()) {
			instance.setId(Long.valueOf(textFieldId.getText()));
		}
		instance.setName(textFieldNombre.getText());
		instance.setLastName(textFieldApellido.getText());
		instance.buildFullName();
		instance.setPending(cmbPendiente.getSelectedNGItem().equals("Si") ? Boolean.TRUE : Boolean.FALSE);

		Email email = instance.getPrimaryEmail();

		if (email == null) {
			email = new Email();
			email.setDoNotUse(Boolean.FALSE);
			email.setPrimaryUse(Boolean.TRUE);
			instance.getEmails().add(email);
		}
		email.setLiteral(textFieldEmail.getText());

		Phone phone = instance.getPrimaryPhone();

		if (phone == null) {
			phone = new Phone();
			phone.setDoNotUse(Boolean.FALSE);
			phone.setPrimaryUse(Boolean.TRUE);
			phone.setPrefix("54");
			instance.getPhones().add(phone);
		}

		phone.setNumber(textFieldTelefono.getText());
		instance.setObservations(textAreaObservaciones.getText());
		// instance.setTags(pickTags.getRightElements());

	}

	@Override
	protected void populateDetailPanel(Customer instance) {

		textFieldId.setText(String.valueOf(instance.getId()));
		textFieldNombre.setText(instance.getName());
		textFieldApellido.setText((instance.getLastName()));
		cmbPendiente.setSelectedNGItem(instance.getPending() ? "Si" : "No");
		textFieldEmail.setText(instance.getPrimaryEmail() == null ? "" : instance.getPrimaryEmail().getLiteral());
		textFieldTelefono.setText(instance.getPrimaryPhone() == null ? "" : instance.getPrimaryPhone().getNumber());
		// rightTags.addAll(instance.getTags());
		textAreaObservaciones.setText(instance.getObservations());
	}

}
